package com.baffle.socket.common;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author MT
 * 模拟客户端
 * @date 2019-12-09 13:26
 */
@Slf4j
public class ChatClient {
    public static void main(String[] args) throws IOException, InterruptedException {

        ExecutorService executorService = Executors.newFixedThreadPool(10);
            TimeUnit.SECONDS.sleep(5);
            executorService.submit(() -> {

                // 要连接的服务端IP地址和端口
                String host = "127.0.0.1";
                int port = 8088;
                // 与服务端建立连接
                Socket socket = null;
                OutputStream outputStream = null;
                InputStream inputStream = null;
                try {
                    socket = new Socket(host, port);
                    // 设置超时时间
                    socket.setSoTimeout(50000);
                    // 建立连接后获得输出流
                    outputStream = socket.getOutputStream();
                    //String message = UUID.randomUUID().toString();
                    
                	String message = "0260{" + " \"HEAD\": {" + " \"MERCH_ID\": \"101601200000601\","
            				+ " \"TRAN_DATETIME\": \"20210601100012\"," + " \"ORDER_ID\": \"20210601100012004950\"" + " },"
            				+ " \"BODY\": {" + " \"DATA\": {" + " \"TRAN_CODE\": \"310002\"," + " \"CURRENCY\": \"156\","
            				+ " \"AMOUNT\": 86677," + " \"PAYER_CARD_NO\": \"6029079165791639\"," + " \"PAYER_NAME\": \"养毅\","
            				+ " \"PAYER_ID_TYPE\": \"01\"," + " \"PAYER_ID_CODE\": \"420101198101019779\","
            				+ " \"PAYER_PHONE_NO\": \"17700001625\"," + " \"PAYER_OB_BANK_CODE\": \"\","
            				+ " \"PAYEE_CARD_NO\": \"749769627778\"," + " \"PAYEE_NAME\": \"开泰银行（中国）有限公司\","
            				+ " \"PAYEE_ID_TYPE\": \"\"," + " \"PAYEE_ID_CODE\": \"\"," + " \"PAYEE_PHONE_NO\": \"\","
            				+ " \"PAYEE_OB_BANK_CODE\": \"\"," + " \"SMS_NO\": \"\"," + " \"SMS_CODE\": \"\","
            				+ " \"DEVICE_IMEI\": \"\"," + " \"IP_USER_ID\": \"\" " + "}" + "}" + "}";
                	
                	
                    byte[] messageBytes = message.getBytes("UTF-8");
                    socket.setSendBufferSize(messageBytes.length);
                    socket.getOutputStream().write(messageBytes);
                    // 必需关闭输出流
                    socket.shutdownOutput();
                    // 获取输出流
                    inputStream = socket.getInputStream();
                    byte[] bytes = new byte[1024];
                    int len;
                    StringBuilder sb = new StringBuilder();
                    while ((len = inputStream.read(bytes)) != -1) {
                        //注意指定编码格式，发送方和接收方一定要统一，建议使用UTF-8
                        sb.append(new String(bytes, 0, len, "UTF-8"));
                    }
                    System.out.println("服务器返回数据: " + sb);

                } catch (IOException e) {
                    e.printStackTrace();

                    try {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                    } catch (IOException exception) {
                        e.printStackTrace();
                    }
                    try {
                        if (outputStream != null) {
                            outputStream.close();
                        }
                    } catch (IOException exception) {
                        e.printStackTrace();
                    }
                    try {
                        if (socket != null) {
                            socket.close();
                        }
                    } catch (IOException exception) {
                        e.printStackTrace();
                    }
                }
            });
        executorService.shutdown();
    }
}
